package com.xzk.controller;

import com.xzk.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Classname UserController
 * @Description /user post新增
 *              /{id} delete 删除
 *              /{id} put 更新
 *              /{id} get 根据id加载
 *              /list-page post 分页查询
 * @Date 2020/10/20 13:34
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */

@RestController
@RequestMapping("/user")
@Api(tags = {"用户管理API"})
public class UserController {

    @PostMapping
    @ApiOperation(value = "新增用户", notes = "新增后返回当前用户")
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回成功", response = User.class),
            @ApiResponse(code = 400, message = "参数未完善(id == 1)", response = User.class),
            @ApiResponse(code = 401, message = "权限不足(id == 2)", response = User.class)
    })
    public ResponseEntity<User> add(User user){
        if(user.getId() == 1){
            return new ResponseEntity<>(user, HttpStatus.BAD_REQUEST);  //400
        }else if(user.getId() == 2){
            return new ResponseEntity<>(user, HttpStatus.UNAUTHORIZED); //401
        }else{
            return ResponseEntity.ok(user);
        }
    }

    @PutMapping
    @ApiOperation(value = "修改用户", notes = "修改后返回当前用户")
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回成功", response = User.class),
            @ApiResponse(code = 400, message = "参数未完善(id == 1)", response = User.class),
            @ApiResponse(code = 401, message = "权限不足(id == 2)", response = User.class)
    })
    public ResponseEntity<User> update(User user){
        if(user.getId() == 1){
            return new ResponseEntity<>(user, HttpStatus.BAD_REQUEST);  //400
        }else if(user.getId() == 2){
            return new ResponseEntity<>(user, HttpStatus.UNAUTHORIZED); //401
        }else{
            return ResponseEntity.ok(user);
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户", notes = "删除后返回当前用户id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回成功", response = User.class),
            @ApiResponse(code = 400, message = "参数未完善(id == 1)", response = User.class),
            @ApiResponse(code = 401, message = "权限不足(id == 2)", response = User.class)
    })
    public ResponseEntity<Long> delete(@PathVariable Long id){
        if(id == 1){
            return new ResponseEntity<>(id, HttpStatus.BAD_REQUEST);  //400
        }else if(id == 2){
            return new ResponseEntity<>(id, HttpStatus.UNAUTHORIZED); //401
        }else{
            return ResponseEntity.ok(id);
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "分页查询用户", notes = "返回分页的数据")
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回成功", response = User.class),
            @ApiResponse(code = 400, message = "参数未完善(id == 1)", response = User.class),
            @ApiResponse(code = 401, message = "权限不足(id == 2)", response = User.class)
    })
    public ResponseEntity<Long> getUserById(@PathVariable Long id){
        if(id == 1){
            return new ResponseEntity<>(id, HttpStatus.BAD_REQUEST);  //400
        }else if(id == 2){
            return new ResponseEntity<>(id, HttpStatus.UNAUTHORIZED); //401
        }else{
            return ResponseEntity.ok(id);
        }
    }

    @PostMapping("/list-page")
    public ResponseEntity<String> findByPage(Integer pageNum, Integer pageSize){
        return ResponseEntity.ok("find page result...");
    }

}
